﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Bussiness
{
    /// <summary>
    /// 工况寻优类
    /// </summary>
    public class Optimization
    {
        /// <summary>
        /// 类别
        /// </summary>
        public string Catalog { get; set; }
        /// <summary>
        /// 参数标示
        /// </summary>
        public string ParaMark { get; set; }
        /// <summary>
        /// 数值
        /// </summary>
        public string OpValue { get; set; }
        /// <summary>
        /// 最小负荷
        /// </summary>
        public string MinBurden { get; set; }
        /// <summary>
        /// 最大负荷
        /// </summary>
        public string MaxBurden { get; set; }
        /// <summary>
        /// 最低温度
        /// </summary>
        public string LowTemperature { get; set; }
        /// <summary>
        /// 最高温度
        /// </summary>
        public string HighTemperature { get; set; }
        /// <summary>
        /// 缺省值
        /// </summary>
        public string DefaultValue { get; set; }
        /// <summary>
        /// 复制
        /// </summary>
        public void Copy()
        {
            string Dsql = "DELETE FROM ADMINISTRATOR.CYHCS1";
            DB2Provider.DataLink Dlink = new DB2Provider.DataLink();
            Dlink.Excute(Dsql);

            string sql = "SELECT 类别,参数标示,数值,最小负荷,最大负荷,最低温度,最高温度 FROM ADMINISTRATOR.YHCS1";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = link.ExcuteRetureTable(sql);

            DB2Provider.DataLink Clink = new DB2Provider.DataLink();
            if(dt!=null && dt.Rows.Count>0){
                foreach(DataRow dr in dt.Rows){
                    string insertSql = "INSERT INTO ADMINISTRATOR.CYHCS1(类别,参数标示,数值,最小负荷,最大负荷,最低温度,最高温度) VALUES (" + dr[0].ToString() + ",'" + dr[1].ToString() + "'," + dr[2].ToString() + "," + dr[3].ToString() + "," + dr[4].ToString() + "," + dr[5].ToString() + "," + dr[6].ToString() + ")";
                    Clink.Excute(insertSql);
                }
            }
        }

        /// <summary>
        /// 返回工况最优值
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">温度</param>
        /// <param name="paraID">参数编号</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        public double GetOptimizationValue(double burden, double temperature, string paraID, string computeID)
        {
            double Optimization = 0;
            string sql = "SELECT 数值 FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示='" + paraID + "'";
            if (computeID == "2")
                sql = "SELECT 数值 FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示='" + paraID + "'";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            Optimization = Convert.ToDouble(link.GetFirstValue(sql));
            return Optimization;
        }
        /// <summary>
        /// 返回工况最优结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">温度</param>
        /// <param name="paraID">参数编号</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        public System.Data.DataTable GetOptimization(double burden, double temperature, string paraID, string computeID)
        {
            string sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示='" + paraID + "'";
            if (computeID == "2")
                sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示='" + paraID + "'";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = link.ExcuteRetureTable(sql);
            return dt;
        }
        /// <summary>
        /// 返回历史最优值
        /// </summary>
        /// <param name="burden"></param>
        /// <param name="temperature"></param>
        /// <param name="paraIDCollection"></param>
        /// <param name="computeID"></param>
        /// <returns></returns>
        public DataTable GetHisOptimization(double burden, double temperature, string paraIDCollection, string computeID)
        {
            string sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 IN (" + paraIDCollection + ")";
            if (computeID == "2")
                sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 IN (" + paraIDCollection + ")";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = link.ExcuteRetureTable(sql);

            return dt;
        }
        /// <summary>
        /// 返回工况最优值结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        public System.Data.DataTable GetOptimization(double burden, double temperature, string computeID)
        {
            string sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " ";
            if (computeID == "2")
                sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " ";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            DataTable dt = link.ExcuteRetureTable(sql);
            return dt;
        }
        /// <summary>
        /// 生成更新列
        /// </summary>
        /// <returns></returns>
        public System.Data.DataTable GenerateUpdateColumn()
        {
            DataTable newDT = new DataTable();

            DataColumn col1 = new DataColumn("ID_KEY");
            DataColumn col2 = new DataColumn("PARANAMEUNIT");
            DataColumn col3 = new DataColumn("OPTIMIZATION");
            DataColumn col4 = new DataColumn("COPTIMIZATION");

            newDT.Columns.Add(col1);
            newDT.Columns.Add(col2);
            newDT.Columns.Add(col3);
            newDT.Columns.Add(col4);

            return newDT;
        }
        /// <summary>
        /// 生成查询结果表列
        /// </summary>
        /// <returns></returns>
        public System.Data.DataTable GenerateOptimizationColumn()
        {
            DataTable newDT = new DataTable();

            DataColumn col1 = new DataColumn("ID_KEY");
            DataColumn col2 = new DataColumn("PARANAMEUNIT");
            DataColumn col3 = new DataColumn("REALTIME");
            DataColumn col4 = new DataColumn("Optimization");
            DataColumn col5 = new DataColumn("DisValue");

            newDT.Columns.Add(col1);
            newDT.Columns.Add(col2);
            newDT.Columns.Add(col3);
            newDT.Columns.Add(col4);
            newDT.Columns.Add(col5);

            return newDT;
        }
        //public ViewOptimization GetUpdataOptimization(double burden, double temperature, string computeID, string paraID,string paraType, PiLink.declare PIDeclare)
        //{
        //    string sql = "";
        //    if (computeID == "1")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 ='" + paraID + "'";
        //    if (computeID == "2")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 ='" + paraID + "'";
        //    DB2Provider.DataLink link = new DB2Provider.DataLink();
        //    DataTable dt = link.ExcuteRetureTable(sql);

        //    ViewOptimization vOp = new ViewOptimization();
        //    SysParament para = new SysParament();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            int catalog = Convert.ToInt32(dr[1]);
        //            if (paraID != "")
        //            {
        //                PIModel model = new PIModel();
        //                vOp.RealValue = model.GetCurrentValue(paraID, PIDeclare).ToString("0.00");

        //                double optimizationValue = Convert.ToDouble(dr[3]);
        //                vOp.Optimization = optimizationValue.ToString("0.00");

        //                if (paraType == "状态")
        //                {
        //                    if (vOp.RealValue == "0.00")
        //                        vOp.RealValue = "停止";
        //                    else if (vOp.RealValue == "-1.00")
        //                        vOp.RealValue = "运行";

        //                    if (vOp.Optimization == "0.00")
        //                        vOp.Optimization = "停止";
        //                    else if (vOp.Optimization == "-1.00")
        //                        vOp.Optimization = "运行";
        //                    vOp.DisValue = vOp.Optimization;
        //                }
        //                else
        //                {
        //                    //if (Convert.ToDouble(vOp.RealValue) < Convert.ToDouble(vOp.Optimization) && paraID == "CSPC:01XN_b_g")
        //                    //    Update(catalog, "CSPC:01XN_b_g", computeID, vOp.RealValue);

        //                    vOp.DisValue = (Convert.ToDouble(vOp.RealValue) - Convert.ToDouble(vOp.Optimization)).ToString("0.00");

        //                }
        //            }
        //        }
        //    }
        //    return vOp;
        //}
        /// <summary>
        /// 返回最优结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="computeID">机组编号</param>
        /// <param name="paraIDCollection">参数编号集</param>
        /// <param name="PIDeclare">PI连接对象</param>
        /// <returns></returns>
        //public ViewOptimization GetUpdataOptimizationByType(double burden, double temperature, string computeID, string paraID, string paraNameUnit,string paraType,PiLink.declare PIDeclare)
        //{
        //    string sql = "";
        //    if (computeID == "1")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 ='"+paraID+"'";
        //    if (computeID == "2")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 ='" + paraID + "'";
        //    DB2Provider.DataLink link = new DB2Provider.DataLink();
        //    DataTable dt = link.ExcuteRetureTable(sql);

        //    ViewOptimization vOp = new ViewOptimization();
        //    SysParament para = new SysParament();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            int catalog = Convert.ToInt32(dr[1]);
        //            vOp.ParaNameUnit = paraNameUnit;
        //            if (paraID != "")
        //            {
        //                PIModel model = new PIModel();
        //                vOp.RealValue = model.GetCurrentValue(paraID, PIDeclare).ToString("0.00");

        //                double optimizationValue = Convert.ToDouble(dr[3]);
        //                vOp.Optimization = optimizationValue.ToString("0.00");

        //                if (paraType == "状态")
        //                {
        //                    if (vOp.RealValue == "0.00")
        //                        vOp.RealValue = "停止";
        //                    else if (vOp.RealValue == "-1.00")
        //                        vOp.RealValue = "运行";

        //                    if (vOp.Optimization == "0.00")
        //                        vOp.Optimization = "停止";
        //                    else if (vOp.Optimization == "-1.00")
        //                        vOp.Optimization = "运行";
        //                    vOp.DisValue = vOp.Optimization;
        //                }
        //                else
        //                {
        //                    if (Convert.ToDouble(vOp.RealValue) < Convert.ToDouble(vOp.Optimization) && paraID == "CSPC:01XN_b_g")
        //                        Update(catalog, "CSPC:01XN_b_g", computeID, vOp.RealValue);
        //                    vOp.DisValue = (Convert.ToDouble(vOp.RealValue) - Convert.ToDouble(vOp.Optimization)).ToString("0.00");
        //                }
        //            }
        //        }
        //    }
        //    return vOp;
        //}
        /// <summary>
        /// 返回最优结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="computeID">机组编号</param>
        /// <param name="paraIDCollection">参数编号集</param>
        /// <param name="PIDeclare">PI连接对象</param>
        /// <returns></returns>
        //public DataTable GetOptimizationByType(double burden, double temperature, string computeID, string paraIDCollection, PiLink.declare PIDeclare)
        //{
        //    string sql="";
        //    if (computeID == "1")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS1 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 IN (" + paraIDCollection + ")";
        //    if (computeID == "2")
        //        sql = "SELECT * FROM ADMINISTRATOR.YHCS2 WHERE 最小负荷<=" + burden + " AND 最大负荷>=" + burden + " AND 最低温度<=" + temperature + " AND 最高温度>=" + temperature + " AND 参数标示 IN (" + paraIDCollection + ")";
        //    DB2Provider.DataLink link = new DB2Provider.DataLink();
        //    DataTable dt = link.ExcuteRetureTable(sql);
            
        //    DataTable newDT = GenerateOptimizationColumn();
        //    SysParament para = new SysParament();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            DataRow newDR = newDT.NewRow();
        //            newDR[0] = dr[0];

        //            int catalog = Convert.ToInt32(dr[1]);
                    
        //            newDR[1] = para.GetParaNameUnit(dr[2].ToString());                    
        //            string tagName = para.GetPIPointName(dr[2].ToString());
        //            if (tagName != "")
        //            {
        //                PIModel model = new PIModel();
                        

        //                string paraID = dr[2].ToString();
        //                string paraType = para.GetParaState(paraID);
        //                newDR[2] = model.GetCurrentValue(tagName, PIDeclare).ToString("0.00");

        //                double optimizationValue=Convert.ToDouble(dr[3]);
        //                newDR[3] = optimizationValue.ToString("0.00");

        //                if (paraType == "状态")
        //                {
        //                    if (newDR[2].ToString() == "0.00")
        //                        newDR[2] = "停止";
        //                    else if (newDR[2].ToString() == "-1.00")
        //                        newDR[2] = "运行";

        //                    if (newDR[3].ToString() == "0.00")
        //                        newDR[3] = "停止";
        //                    else if (newDR[3].ToString() == "-1.00")
        //                        newDR[3] = "运行";
        //                    newDR[4] = newDR[3];
        //                }
        //                else
        //                {
        //                    if (Convert.ToDouble(newDR[2]) < Convert.ToDouble(newDR[3]) && dr[2].ToString()=="CSPC:01XN_b_g")
        //                        Update(catalog, "CSPC:01XN_b_g", computeID, newDR[2].ToString());
        //                    newDR[4] = (Convert.ToDouble(newDR[2]) - Convert.ToDouble(newDR[3])).ToString("0.00");
        //                }   
        //            }
                    
        //            newDT.Rows.Add(newDR);
        //        }
        //    }
        //    return newDT;
        //}
        /// <summary>
        /// 返回查询最优结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="paraID">参数编号</param>
        /// <param name="PIDeclare">PI连接对象</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        //public System.Data.DataTable SearchOptimization(double burden, double temperature,string computeID, string paraID, PiLink.declare PIDeclare)
        //{
        //    DataTable dt = GetOptimization(burden, temperature, paraID,computeID);
        //    DataTable newDT = GenerateOptimizationColumn();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            DataRow newDR = newDT.NewRow();
        //            newDR[0] = dr[0];

        //            SysParament para = new SysParament();
        //            newDR[1] = para.GetParaNameUnit(paraID);

        //            string tagName = para.GetPIPointName(paraID);
        //            if (tagName != "")
        //            {
        //                PIModel model = new PIModel();
        //                newDR[2] = model.GetCurrentValue(tagName, PIDeclare);
        //            }

        //            newDR[3] = dr[3];
        //            newDT.Rows.Add(newDR);
        //        }
        //    }
        //    return newDT;
        //}
        /// <summary>
        /// 返回历史寻优结果集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="paraID">参数编号</param>
        /// <param name="hisTime">历史时间</param>
        /// <param name="PIDeclare">PI连接对象</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        //public System.Data.DataTable GetHistoryOptimization(double burden, double temperature, string paraIDCollection, string hisTime, PiLink.declare PIDeclare, string computeID)
        //{
        //    // 获取历史最优数据
        //    DataTable dt = GetHisOptimization(burden, temperature, paraIDCollection, computeID);
        //    DataTable newDT = GenerateOptimizationColumn();
        //    // 生成最优查询显示数据
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        foreach (DataRow dr in dt.Rows)
        //        {
        //            DataRow newDR = newDT.NewRow();
        //            newDR[0] = dr[0];

        //            string paraID = dr[2].ToString();

        //            SysParament para = new SysParament();
        //            newDR[1] = para.GetParaNameUnit(paraID);

        //            string tagName = para.GetPIPointName(paraID);
        //            if (tagName != "")
        //            {
        //                PIModel model = new PIModel();
        //                newDR[2] = model.GetHistory(tagName, hisTime, PIDeclare).ToString("0.00");
        //                newDR[3] = Convert.ToDouble(dr[3]).ToString("0.00");
        //                string paraType = para.GetParaState(paraID);
        //                if (paraType == "状态")
        //                {
        //                    if (newDR[2].ToString() == "0.00")
        //                        newDR[2] = "停止";
        //                    else if (newDR[2].ToString() == "-1.00")
        //                        newDR[2] = "运行";

        //                    if (newDR[3].ToString() == "0.00")
        //                        newDR[3] = "停止";
        //                    else if (newDR[3].ToString() == "-1.00")
        //                        newDR[3] = "运行";
        //                }
        //            }

                    
        //            newDT.Rows.Add(newDR);
        //        }
        //    }
        //    return newDT;
        //}
        ///// <summary>
        /// 返回更新最优集
        /// </summary>
        /// <param name="burden">负荷</param>
        /// <param name="temperature">水温</param>
        /// <param name="computeID">机组编号</param>
        /// <returns></returns>
        public System.Data.DataTable GetUpdateOptimization(double burden, double temperature, string computeID)
        {
            DataTable dt = GetOptimization(burden, temperature, computeID);
            DataTable newDT = GenerateUpdateColumn();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DataRow newDR = newDT.NewRow();
                    newDR[0] = dr[0];

                    SysParament para = new SysParament();
                    newDR[1] = para.GetParaNameUnit(dr[2].ToString());

                    newDR[2] = Convert.ToDouble(dr[3]).ToString("0.00");

                    newDR[3] = GetDefaultOptimization(dr[1].ToString(), dr[2].ToString(), computeID);
                    newDT.Rows.Add(newDR);
                }
            }
            return newDT;
        }
        /// <summary>
        /// 获取默认最优值
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="paraid"></param>
        /// <param name="computeID"></param>
        /// <returns></returns>
        public string GetDefaultOptimization(string catalog,string paraid,string computeID)
        {
            string sql = "SELECT 数值 FROM ADMINISTRATOR.CYHCS1 WHERE 类别="+catalog+" AND 参数标示='"+paraid+"'";
            if(computeID=="2")
                sql = "SELECT * FROM ADMINISTRATOR.CYHCS2 WHERE 类别=" + catalog + " AND 参数标示='" + paraid + "'";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            string v = "";
            try
            {
                object o = link.GetFirstValue(sql);
                if (o != null && o != DBNull.Value)
                    v = Convert.ToDouble(o).ToString("0.00");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
            return v;
        }
        /// <summary>
        /// 更新最优值
        /// </summary>
        /// <param name="ID_KEY"></param>
        /// <param name="computeID"></param>
        /// <param name="optizimationValue"></param>
        public void Update(int ID_KEY, string computeID, double optizimationValue)
        {
            string sql = "UPDATE ADMINISTRATOR.YHCS1 SET 数值=" + optizimationValue + " WHERE ID_KEY=" + ID_KEY;
            if (computeID == "2")
                sql = "UPDATE ADMINISTRATOR.YHCS2 SET 数值=" + optizimationValue + " WHERE ID_KEY=" + ID_KEY;
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }
        /// <summary>
        /// 删除最优数据
        /// </summary>
        /// <param name="ID_KEY">主键ID</param>
        /// <param name="computeID">机组编号</param>
        public void Delete(int ID_KEY, string computeID)
        {
            string sql = "DELETE FROM ADMINISTRATOR.YHCS1 WHERE ID_KEY=" + ID_KEY;
            if(computeID=="2")
                sql = "DELETE FROM ADMINISTRATOR.YHCS2 WHERE ID_KEY=" + ID_KEY;
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }
        /// <summary>
        /// 更新最优值
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="piname"></param>
        /// <param name="computeID"></param>
        /// <param name="optizimation"></param>
        public void Update(int catalog, string piname, string computeID, string optizimation)
        {
            string sql = "UPDATE ADMINISTRATOR.YHCS1 SET 数值=" + optizimation + " WHERE 参数标示='" + piname + "' AND 类别 =" + catalog ;
            if (computeID == "2")
                sql = "UPDATE ADMINISTRATOR.YHCS2 SET 数值=" + optizimation + " WHERE 参数标示='" + piname + "' AND 类别 =" + catalog;
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }
    }

    public class ViewOptimization
    {
        /// <summary>
        /// 参数名称单位
        /// </summary>
        public string ParaNameUnit { get; set; }
        /// <summary>
        /// 实时值
        /// </summary>
        public string RealValue { get; set; }
        /// <summary>
        /// 寻优值
        /// </summary>
        public string Optimization { get; set; }
        /// <summary>
        /// 差值
        /// </summary>
        public string DisValue { get; set; }
    }
}
